import Vue from "vue";
import Router from "vue-router";
import { routes } from "./routes";

// 重复点击路由错误
const originalPush = Router.prototype.push;

Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(Router);

const router = new Router({
  // mode: "history",
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition && to.meta.keepAlive) {
      // return new Promise((resolve) => {
      //   setTimeout(() => {
      //     resolve(savedPosition);
      //   }, 120);
      // });
      return savedPosition;
    } else {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ x: 0, y: 0 });
        }, 5);
      });
    }
  },
});

export default router;
